package com.hub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hub.dto.PageParam;
import com.hub.dto.auth.MenuParam;
import com.hub.entity.AuthMenu;
import com.hub.frame.consts.ConstantUtil;
import com.hub.utils.ApiResultObject;
import com.hub.mapper.AuthMenuMapper;
import com.hub.model.auth.TreeBean;
import com.hub.service.IAuthMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hub
 * @since 2020-05-14
 */
@Service
public class AuthMenuServiceImpl extends BaseServiceImpl<AuthMenuMapper, AuthMenu> implements IAuthMenuService {

    @Resource
    private AuthMenuMapper authMenuMapper;

    @Override
    public ApiResultObject getMenuTree() {
        List<TreeBean> list = authMenuMapper.selectMenuTree();
        TreeBean treeBean = new TreeBean();
        treeBean.setId(ConstantUtil.INIT_MENU_ID);
        treeBean.setLabel(ConstantUtil.INIT_MENU_NAME);
        treeBean.setLevel(ConstantUtil.INIT_MENU_LV);
        treeBean.setTreeBeans(list);
        List<TreeBean> result = new ArrayList<>();
        result.add(treeBean);
        return ApiResultObject.success(result);
    }

    @Override
    public ApiResultObject getList(PageParam<AuthMenu> pageParam) {
        Page<AuthMenu> page = pageParam.getPage();
        AuthMenu param = pageParam.getParams();
        return ApiResultObject.success(authMenuMapper.selectListByPage(page, param));
    }

    @Override
    public ApiResultObject createMenu(MenuParam param) {
        QueryWrapper<AuthMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_name", param.getMenuName());
        wrapper.eq("active_flg", ConstantUtil.YES);
        if(this.count(wrapper) > 0){
            return ApiResultObject.failed("菜单名已存在，请重新输入！");
        }
        wrapper = new QueryWrapper<>();
        wrapper.eq("permisson", param.getPermisson());
        wrapper.eq("active_flg", ConstantUtil.YES);
        if(this.count(wrapper) > 0){
            return ApiResultObject.failed("权限码已存在，请重新输入！");
        }
        AuthMenu authMenu = new AuthMenu();
        authMenu.setMenuName(param.getMenuName());
        authMenu.setParentId(param.getParentId());
        authMenu.setPermisson(param.getPermisson());
        authMenu.setLevel(param.getLevel());
        authMenu.setSort(param.getSort());
        authMenu.setType(param.getType());
        authMenu.setRemark(param.getRemark());
        this.save(authMenu);
        return ApiResultObject.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResultObject updateMenu(Long id, MenuParam param) {
        //检查菜单名
        QueryWrapper<AuthMenu> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param.getMenuName())){
            wrapper.eq("menu_name", param.getMenuName());
            wrapper.ne("id", id);
            wrapper.eq("active_flg", ConstantUtil.YES);
            if(this.count(wrapper) > 0){
                return ApiResultObject.failed("菜单名已存在，请重新输入！");
            }
        }
        //检查权限码
        if(StringUtils.isNotEmpty(param.getPermisson())){
            wrapper = new QueryWrapper<>();
            wrapper.eq("permisson", param.getPermisson());
            wrapper.ne("id", id);
            wrapper.eq("active_flg", ConstantUtil.YES);
            if(this.count(wrapper) > 0){
                return ApiResultObject.failed("权限码已存在，请重新输入！");
            }
        }
        AuthMenu authMenu = new AuthMenu();
        authMenu.setId(id);
        authMenu.setMenuName(param.getMenuName());
        authMenu.setPermisson(param.getPermisson());
        authMenu.setParentId(param.getParentId());
        authMenu.setSort(param.getSort());
        authMenu.setLevel(param.getLevel());
        authMenu.setType(param.getType());
        authMenu.setRemark(param.getRemark());
        authMenu.setActiveFlg(ConstantUtil.YES);
        if(this.updateById(authMenu)){
            return ApiResultObject.success();
        }
        return ApiResultObject.failed();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResultObject deleteMenu(Long id) {
        QueryWrapper<AuthMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        if(this.count(wrapper) > 0){
            ApiResultObject.failed("该菜单存在子菜单，不可删除！");
        }
        if(this.removeById(id)){
            return ApiResultObject.success();
        }
        return ApiResultObject.failed();
    }
}
